//
//  GuideView.m
//  echo
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 Danny. All rights reserved.
//

#import "GuideView.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation GuideView 

- (void)dealloc{
    [_pageControl release];
    [_buttonForInto release];
    self.scrollView.delegate = nil;
    [_scrollView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<NSString *> *)images{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]initWithFrame:frame];
        [self addSubview:self.scrollView];
        self.scrollView.delegate = self;
        [self.scrollView release];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.contentSize = CGSizeMake(frame.size.width * images.count, frame.size.height);
        self.scrollView.showsVerticalScrollIndicator = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self createImages:images frame:frame];
        
        self.buttonForInto = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.scrollView addSubview:self.buttonForInto];
        self.buttonForInto.frame = CGRectMake(self.frame.size.width * (images.count - 1) + self.frame.size.width / 2 - 60, self.frame.size.height - 150, 120, 40);
        self.buttonForInto.backgroundColor = [UIColor clearColor];
        self.buttonForInto.layer.cornerRadius = 20;
        self.buttonForInto.layer.masksToBounds = YES;
        [self.buttonForInto setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.buttonForInto.layer.borderColor = [UIColor greenColor].CGColor;
        self.buttonForInto.layer.borderWidth = 1.2;
        [self.buttonForInto setTitle:@"进入我的灵音" forState:UIControlStateNormal];
    }
    return self;
}

- (void)createImages:(NSArray *)images frame:(CGRect)frame{
    for (int i = 0; i < images.count; i ++ ) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
        [image sd_setImageWithURL:[NSURL URLWithString:[images objectAtIndex:i]]];
        [self.scrollView addSubview:image];
    }
    [self createPageControlWithImages:images frame:frame];
}

- (void)createPageControlWithImages:(NSArray *)images frame:(CGRect)frame{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(frame.size.width/2 - 50, frame.size.height - 80, 100, 38)];
    self.pageControl.numberOfPages = images.count;
    [self addSubview:self.pageControl];
    self.pageControl.backgroundColor = [UIColor clearColor];
    [self.pageControl addTarget:self action:@selector(handlePageControl) forControlEvents:UIControlEventValueChanged];
}

- (void)handlePageControl{
    [self.scrollView setContentOffset:CGPointMake(self.pageControl.currentPage * self.frame.size.width , 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.frame.size.width;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
